package cn.openea.perpartner.cms.web.controller;

import cn.openea.perpartner.cms.annotation.CmsLog;
import cn.openea.perpartner.cms.enums.OperatorType;
import cn.openea.perpartner.cms.web.entity.CmsCustomer;
import cn.openea.perpartner.cms.web.service.ICmsCommodityService;
import cn.openea.perpartner.cms.web.service.ICmsCustomerService;
import cn.openea.perpartner.cms.web.service.ICmsSupplementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import cn.openea.perpartner.cms.web.dto.AssignDto;
import cn.openea.perpartner.cms.web.dto.CustomerDto;
import cn.openea.perpartner.cms.web.entity.CmsUser;
import cn.openea.perpartner.cms.web.service.ICmsUserService;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * (CmsCustomer)表控制层
 *
 * @author makejava
 * @since 2022-07-19 22:15:31
 */
@Api(tags = "品牌商家")
@Controller
@RequestMapping("customer")
public class CmsCustomerController extends BaseController {
    @Autowired
    private ICmsCustomerService customerService;
    @Autowired
    private ICmsSupplementService supplementService;
    @Autowired
    private ICmsCommodityService commodityService;
    @Autowired
    private ICmsUserService userService;


    /**
     * 客户列表
     */
    @ApiOperation("客户列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(String userId,String searchStr){
        startPage();
        List<CmsCustomer> list = customerService.selectList(userId,searchStr);
        return  getDataTable(list);
    }
    /**
     * 导入数据
     */
    @ApiOperation("导入Excel")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<CmsCustomer> util = new ExcelUtil<CmsCustomer>(CmsCustomer.class);
        List<CmsCustomer> customerList = util.importExcel(file.getInputStream());
        String message = customerService.importCustomer(customerList);
        return AjaxResult.success(message);
    }
    @ApiOperation(value = "下载模板",produces = "application/octet-stream")
    @PostMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<CmsCustomer> util = new ExcelUtil<CmsCustomer>(CmsCustomer.class);
       return util.importTemplateExcel("客户数据模板");
    }
    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("查看详情")
    @PostMapping("/query")
    @ResponseBody
    public AjaxResult queryById(Long id) {
        return success(this.customerService.queryById(id));
    }

    @ApiOperation("品牌明细")
    @GetMapping("{customerId}")
    @ResponseBody
    public AjaxResult selectOne(@PathVariable Long  customerId) {
        return success(this.customerService.queryById(customerId));
    }
    /**
     * 编辑数据
     * @param customerDto 实体
     * @return 编辑结果
     */
    @ApiOperation("保存商家")
    @CmsLog(title = "品牌商家",operatorType = OperatorType.UPDATE )
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestBody CustomerDto customerDto) {
        return customerService.save(customerDto)?success():error();
    }


    @ApiOperation("导出Excel")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String userId)
    {
        List<CmsCustomer> list = customerService.selectList(userId,"");
        ExcelUtil<CmsCustomer> util = new ExcelUtil<>(CmsCustomer.class);
        return util.exportExcel( list, "客户数据");
    }

    @ApiOperation("回收任务")
    @CmsLog(title = "品牌商家",operatorType = OperatorType.RECYCLE )
    @PostMapping("/recycle")
    @ResponseBody
    public AjaxResult recycleByIds(@RequestBody  List<Long> idList)
    {
       return customerService.recycle(idList)?success("回收成功！"):error("回收失败！");
    }
    @ApiOperation("指派任务")
    @CmsLog(title = "品牌商家",operatorType = OperatorType.ASSIGN )
    @PostMapping("/assign")
    @ResponseBody
    public AjaxResult assign( @RequestBody List<AssignDto> assignList) throws Exception {
        return customerService.assign(assignList)?success("指派成功！"):error("指派失败！");
    }


    @ApiOperation("工作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "0:当前周，-1:上周，+1:下周" )
    })
    @PostMapping("/log/list")
    @ResponseBody
    public TableDataInfo logList(int offset)
    {
        startPage();
        List<Object> list = customerService.selectLogList(offset);
        return getDataTable(list);
    }
    @ApiOperation("指派用户")
    @PostMapping("/user/list")
    @ResponseBody
    public TableDataInfo selectUserList(String searchStr){
        startPage();
         List<CmsUser> list = userService.selectUserList(searchStr);
        return getDataTable(list);
    }

    @ApiOperation("申请签约")
    @GetMapping("/contract/apply/{customerId}")
    @ResponseBody
    public AjaxResult applyContract(@PathVariable String customerId){
        return customerService.applyContract(customerId)?success():error();
    }
    @ApiOperation("通过签约")
    @GetMapping("/contract/approve/{customerId}")
    @ResponseBody
    public AjaxResult approveContract(@PathVariable String customerId){
        return customerService.approveContract(customerId)?success():error();
    }
    @ApiOperation("拒绝签约")
    @GetMapping("/contract/refuse/{customerId}")
    @ResponseBody
    public AjaxResult refuseContract(@PathVariable String customerId,String context){
        return  customerService.refuseContract(customerId,context)?success():error();
    }

    @ApiOperation("申请列表")
    @PostMapping("/contract/list")
    @ResponseBody
    public TableDataInfo list(String searchStr){
        startPage();
        List<CmsCustomer> list = customerService.selectList(searchStr);
        return  getDataTable(list);
    }

    @ApiOperation("签约校验")
    @GetMapping("/contract/check/{customerId}")
    @ResponseBody
    public AjaxResult check(@PathVariable String customerId){
        if (!customerService.check(customerId)){
            return error("请完善品牌商家信息！");
        }
        if (!supplementService.check(customerId)){
            return error("请完善品牌店铺信息！");
        }
        return  commodityService.check(customerId)?success():error("请完善签约商品信息！");
    }

}

